package com.shop.dingdongshoppingapi.controller;

import com.shop.dingdongshoppingapi.dto.AliPayDto;
import com.shop.dingdongshoppingapi.util.AliPayUtil;
import com.shop.dingdongshoppingapi.util.StringUtil;
import com.shop.dingdongshoppingapi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * Author:小樊
 *
 * @Date:2022/7/4 21:23
 */
@RestController
@CrossOrigin
@Api(tags = "支付")
@RequestMapping("/api/pay/")
public class PayController {
    //创建支付
    @PostMapping("createpay.do")
    @ApiOperation(value = "创建支付")
    public R create(@RequestBody AliPayDto dto){
        String res = AliPayUtil.createPay(dto.getNo(),dto.getMoney(),dto.getTitle());
        if(StringUtil.isNotEmpty(res)){
            return R.ok(res);
        }else {
            return R.fail("支付失败");
        }

        }
    //查询支付状态
    @GetMapping("query.do")
    @ApiOperation(value = "查询支付状态")
    public R query(String no){
        return R.ok(AliPayUtil.queryPay(no));

    }

    //发起退款
    @GetMapping("refund.do")
    @ApiOperation(value = "发起退款")
    public R refund(String no,double money){
        if(AliPayUtil.refound(no,money)){
            return R.ok();
        }
        return R.fail();
    }
}
